﻿using System.Text;
using System.Text.RegularExpressions;
using LuckyShell.ShellOrders;

namespace LuckyShell.ShellValues;

public class InterpolateStringValue(string value) : ShellValue
{
    public object GetValue(ref VariateManager manager)
    {
        var list = Value.Split(" ").Where(x => !string.IsNullOrEmpty(x));
        var builder = new StringBuilder();
        var regex = new Regex("(.*)@(.*)");
        foreach (var s in list)
        {
            if (string.IsNullOrEmpty(s)) continue;
            if (regex.IsMatch(s))
            {
                var g = regex.Match(s);
                if (g.Groups.Count != 3)
                    builder.Append(" " + s);
                else
                {
                    var v = manager.GetValue(new IdentifierValue(g.Groups[2].Value));
                    builder.Append($" {g.Groups[1].Value}{v}");
                }
            }
            else
            {
                builder.Append(" " + s);
            }
        }

        return builder.ToString();
    }

    private string Value { get; set; } = value;
}